package DCSource.ClosedPlugin;

import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

import javax.swing.JFrame;

import DCSource.ConfigManager.MGm;
import DCSource.ConfigManager.Manager;

public class processList {
	public processList(){};
	private ArrayList<ClosedPlugin> list = new ArrayList<ClosedPlugin>();
	private ArrayList<JFrame> list2 = new ArrayList();
	private ArrayList<Thread> list3 = new ArrayList();
	private Manager settings = new Manager("ClosedPluginSettings");
	public void loadFolder(String folder,boolean older){
		PLReader reader = new PLReader();
		reader.loadFolder(settings, folder, older);
		((processList)(this)).add(reader.toArrayList());
	}
	public void loadFile(String file,boolean older){
		PLReader reader = new PLReader();
		((processList)(this)).add(reader.getFromFile(settings, file, older));
	}
	public MGm loadFolderWithInformation(String folder,boolean older){
		PLReader reader = new PLReader();
		reader.loadFolder(settings, folder, older);
		((processList)(this)).add(reader.toArrayList());
		return reader.getInfo();
	}
	public void add(ClosedPlugin M){
		list.add(M);
	}
	public void add(ClosedPlugin[] M){
		for (int I=0;I<M.length;I++){
			add(M[I]);
		}
	}
	public void add(ArrayList<ClosedPlugin> M){
		for (int I=0;I<M.size();I++){
			add(M.get(I));
		}
	}
	public void add(Thread t){
		list3.add(t);
	}
	public void rev(JFrame M){
		for (int I = 0;I<list2.size();I++){
			if (M.equals(list2.get(I))){
				try{
					list2.get(I).dispose();
				}catch(Exception e){}
				list2.remove(I);
			}
		}
	}
	public void rev(Thread M){
		for (int I = 0;I<list3.size();I++){
			if (M.equals(list3.get(I))){
				M.stop();
				list3.remove(I);
			}
		}
	}
	public void revPlugin(final int DEX){
		final processList b = (processList) this;
		if (DEX>=0 & DEX<list.size()){
			list.get(DEX).onExit(settings,b);
			list.remove(DEX);
		}
	}
	public void clear(){
		closeProcess();
		list.clear();
		list2.clear();
		list3.clear();
	}
	public void rev(ClosedPlugin M){
		final processList b = (processList) this;
		for (int I = 0;I<list.size();I++){
			if (M.equals(list.get(I))){
				list.get(I).onExit(settings,b);
				list.remove(I);
			}
		}
	}
	public void startProcess(){
		final processList b = (processList) this;
		for (int I = 0;I<list.size();I++){
			list.get(I).onStart(settings,b);
		}
	}
	public void refreshProcess(){
		final processList b = (processList) this;
		for (int I = 0;I<list.size();I++){
			list.get(I).onRefresh(settings);
		}
	}
	public void closeProcess(){
		final processList b = (processList) this;
		ArrayList<Thread> listen = new ArrayList<Thread>();
		exitRefresh();
		for (int I = 0;I<list.size();I++){
			list.get(I).onExit(settings,b);
			list.get(I).onReset(settings);
		}
		for (int I = 0;I<list2.size();I++){
			try{
				list2.get(I).dispose();
			}catch(Exception e){}
		}
		for (int I = 0;I<list3.size();I++){
			try{
				list3.get(I).stop();
			}catch(Exception e){}
		}
		list2.clear();
	}
	public void reset(){
		final processList b = (processList) this;
		ArrayList<Thread> listen = new ArrayList<Thread>();
		for (int I = 0;I<list.size();I++){
			list.get(I).onExit(settings,b);
			list.get(I).onReset(settings);
			list.get(I).onStart(settings,b);
		}
	}
	public void add(JFrame frame){
		list2.add(frame);
	}
	public void miniProcess(){
		final processList b = (processList) this;
		ArrayList<Thread> listen = new ArrayList<Thread>();
		for (int I = 0;I<list.size();I++){
			list.get(I).onMinimide(settings);
		}
	}
	public void showProcess(){
		final processList b = (processList) this;
		ArrayList<Thread> listen = new ArrayList<Thread>();
		for (int I = 0;I<list.size();I++){
			list.get(I).onShow(settings);
		}
	}
	public String[] printCommand(final String[] cmd){
		final ArrayList<String> lists = new ArrayList<String>();
		ArrayList<Thread> listen = new ArrayList<Thread>();
		final processList b = (processList) this;
		for (int I = 0;I<list.size();I++){
			lists.add(list.get(I).onCommand(settings,b,cmd));
		}
		if (lists.size()>1){
		return (String[]) lists.toArray();}else{return new String[]{};}
	}
	public String printCommandTo(String title,final String[] cmd){
		final ArrayList<String> lists = new ArrayList<String>();
		ArrayList<Thread> listen = new ArrayList<Thread>();
		final processList b = (processList) this;
		for (int I = 0;I<list.size();I++){
			if (list.get(I).getTitle(settings).equals(title)){
				 lists.add(list.get(I).onCommand(settings,b,cmd));
			}
		}
		if (lists.size()>1){
		return lists.get(lists.size()-1);}else{return "";}
	}
	Thread refresh = new refresh((processList) this);
	public void initRefresh(){
		if (refresh.isAlive()==false){
			try{
			refresh.start();}catch(Exception err){}
		}
	}
	public void exitRefresh(){
		if (refresh.isAlive()==true){
			refresh.stop();
			refresh = new refresh((processList) this);
		}
	}
	
	public ArrayList<ClosedPlugin> getPlugins(){
		return list;
	}
	public WindowListener getWindowListener(final processList b){
		return new WindowListener(){
			public void windowActivated(WindowEvent arg0) {
				Thread doing = new Thread(){
					public void run(){
						b.showProcess();
					}
				};
				doing.start();
			}
			public void windowClosed(WindowEvent arg0) {
				
			}
			public void windowClosing(WindowEvent arg0) {
				Thread doing = new Thread(){
					public void run(){
						b.closeProcess();
					}
				};
				doing.start();
			}
			public void windowDeactivated(WindowEvent arg0) {
				Thread doing = new Thread(){
					public void run(){
						b.miniProcess();
					}
				};
				doing.start();
			}
			public void windowDeiconified(WindowEvent arg0) {
				
			}
			public void windowIconified(WindowEvent arg0) {
				
			}
			public void windowOpened(WindowEvent arg0) {
			
			}};
	}
}
class refresh extends Thread{
	private processList b;
	public refresh(processList b){this.b = b;};
	public void run(){
		while(true){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
			}
			b.refreshProcess();
		}}
}